﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using System.Web.SessionState;


namespace LCrs.Web.personeinfo
{
    /// <summary>
    /// GetEdu 的摘要说明
    /// </summary>
    public class GetEdu : IHttpHandler, IRequiresSessionState
    {
        LCrs.BLL.eduinfo bll = new LCrs.BLL.eduinfo();

        public void ProcessRequest(HttpContext context)
        {
            string ac = "";
            if (null != context.Request.Form["action"])
            {//获取前台传来的值  
                ac = context.Request.Form["action"].ToString().Trim();
            }
            else if (null != context.Request.QueryString["action"])
            {
                ac = context.Request.QueryString["action"].ToString().Trim();
            }
            if (ac == "query")
                Query(context);

            else if (ac == "add")
                add(context);
            else if (ac == "modify")
                modify(context);
            else if (ac == "delete")
                delete(context);
            else if (ac == "deletes")
                deletes(context);

        }
        public void deletes(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                string uidstr = context.Request.QueryString["uids"];
                if (bll.DeleteList(uidstr))
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();

                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();

                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }

        }
        public void delete(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                int uid = 0;
                string uidstr = context.Request.QueryString["uid"];
                if (uidstr != "")
                    uid = int.Parse(uidstr);
                if (bll.Delete(uid))
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }
        }

        public void modify(HttpContext context)
        {
            if (Web.Admin.myinfo.isLogin(context))
            {
                context.Response.ContentType = "text/plain";
                int pid = 0;
                if (context.Request.Form["eid"] != null)
                    int.TryParse(context.Request.Form["eid"], out pid);
                if (pid != 0)
                {
                    DateTime sdate = DateTime.Parse(context.Request.Form["sdate"]);
                    DateTime edate = DateTime.Parse(context.Request.Form["edate"]);
                    string eschool = context.Request.Form["eschool"];
                    string edumajor = context.Request.Form["edumajor"];
                    string edudegree = context.Request.Form["edudegree"];
                    string education = context.Request.Form["education"];
                    string edutype = context.Request.Form["edutype"];
                    string stutype = context.Request.Form["stutype"];                   
                    
                    LCrs.Model.eduinfo model = new LCrs.Model.eduinfo();
                    model.sdate = sdate;
                    model.edate = edate;
                    model.eschool = eschool;
                    model.edumajor = edumajor;
                    model.edudegree = edudegree;
                    model.education = education;
                    model.edutype = edutype;
                    model.stutype = stutype;                    
                    model.eid = pid;                    
                    if (bll.Update(model))
                    {
                        context.Response.Write("T");//返回给前台页面  
                        context.Response.End();
                    }
                    else
                    {
                        context.Response.Write("F");//返回给前台页面  
                        context.Response.End();
                    }
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }

        }

        public void add(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                int pid = 0;
                if (context.Request.QueryString["pid"] != null)
                    int.TryParse(context.Request.QueryString["pid"], out pid);
                if (pid != 0)
                {
                    
                    DateTime inputtime = DateTime.Now;
                    DateTime sdate = DateTime.Parse(context.Request.Form["sdate"]);
                    DateTime edate = DateTime.Parse(context.Request.Form["edate"]);
                    string eschool = context.Request.Form["eschool"];
                    string edumajor = context.Request.Form["edumajor"];
                    string edudegree = context.Request.Form["edudegree"];
                    string education = context.Request.Form["education"];
                    string edutype = context.Request.Form["edutype"];
                    string stutype = context.Request.Form["stutype"];

                    LCrs.Model.eduinfo model = new LCrs.Model.eduinfo();
                    model.sdate = sdate;
                    model.edate = edate;
                    model.eschool = eschool;
                    model.edumajor = edumajor;
                    model.edudegree = edudegree;
                    model.education = education;
                    model.edutype = edutype;
                    model.stutype = stutype;
                    model.pid = pid;
                    model.inputtime = inputtime;

                    int rid = bll.Add(model);
                    if (rid > 0)
                    {
                        context.Response.Write("T");//返回给前台页面  
                        context.Response.End();
                    }
                    else
                    {
                        context.Response.Write("F");//返回给前台页面  
                        context.Response.End();
                    }
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }

        }

        public void Query(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                int uid = Web.Admin.myinfo.getID(context);
                string uname, utruename, st;
                uname = utruename = st = "";
                int pid = 0;
                if (null != context.Request.Form["pid"])
                {
                    pid = int.Parse(context.Request.Form["pid"].ToString().Trim());
                }
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }
                StringBuilder strWhere = new StringBuilder();
                if (pid != 0)
                {
                    strWhere.Append(" pid=" + pid);
                }
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                if (sort != "" && order != "")
                {
                    oderby = order + " " + sort;
                }

                //调用分页的GetList方法  
                DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}